package cn.lsh.mapper;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.StringTokenizer;

public class WordMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
	/**
	 * keyout,valueout对象再外面创建，避免map循环重复创建对象，MapReduce会对写入对象做序列化，不会发生引用传递值发生改变问题
	 */
	private Text word = new Text();

	private static final IntWritable ONE = new IntWritable(1);

	@Override
	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
		//key为这条记录的字节序列号，value这行记录的值,如：hello hadoop 1
		StringTokenizer st = new StringTokenizer(value.toString());
		while (st.hasMoreTokens()) {
			word.set(st.nextToken());
			context.write(word, ONE);
		}
	}
}
